package com.zerui.manager.app.accessauthpersonnel.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zerui.basedata.client.PersonnelService;
import com.zerui.basedata.client.cmd.PersonnelQueryPageCmd;
import com.zerui.basedata.client.dto.PersonnelDTO;
import com.zerui.cola.dto.MultiResponse;
import com.zerui.cola.dto.PageResponse;
import com.zerui.manager.accessauthpersonnel.AccessAuthPersonnel;
import com.zerui.manager.accessauthpersonnel.gateway.AccessAuthPersonnelGateWay;
import com.zerui.manager.app.accessauthpersonnel.convertor.AccessAuthPersonnelVoConvert;
import com.zerui.manager.web.cmd.AccessAuthPersonnelQryCmd;
import com.zerui.manager.web.vo.AccessAuthPersonnelVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author wh
 * @create_time 2023-08-30 14:09
 */
@Component
@CatchAndLog
public class AccessAuthPersonnelPageCmdExe {

    @DubboReference
    private PersonnelService personnelService;
    @Resource
    private AccessAuthPersonnelGateWay accessAuthPersonnelGateWay;
    //public static final String IC_CODE = "IC卡号";

    public PageResponse<AccessAuthPersonnelVO> execute(AccessAuthPersonnelQryCmd accessAuthPersonnelQryCmd) {
        PersonnelQueryPageCmd cmd = new PersonnelQueryPageCmd();
        cmd.setName(accessAuthPersonnelQryCmd.getName());
        cmd.setNumberOrName(accessAuthPersonnelQryCmd.getNumber());
        cmd.setOrgId(accessAuthPersonnelQryCmd.getOrgId());
        cmd.setImage(true);
        cmd.setChildOrg(true);
        cmd.setPageIndex(accessAuthPersonnelQryCmd.getPageIndex());
        cmd.setPageSize(accessAuthPersonnelQryCmd.getPageSize());
        PageResponse<PersonnelDTO> dtoPageResponse = personnelService.pagePersonnel(cmd);
        List<AccessAuthPersonnelVO> accessAuthPersonnelVOS = new ArrayList<>(16);
        if(ObjectUtils.isNotEmpty(dtoPageResponse) && CollectionUtils.isNotEmpty(dtoPageResponse.getData())){
            accessAuthPersonnelVOS = AccessAuthPersonnelVoConvert.INSTANCE.covertPersonnelDTOToAccessAuthPersonnelVO(dtoPageResponse.getData());
            //授权绑定查询
            List<Long> personIds = accessAuthPersonnelVOS.stream()
                    .map(AccessAuthPersonnelVO::getId)
                    .collect(Collectors.toList());
            List<AccessAuthPersonnel> byPersonnelIds = accessAuthPersonnelGateWay.getByPersonnelIds(personIds);
            if(CollectionUtils.isNotEmpty(byPersonnelIds)){
                Map<Long, Long> map = byPersonnelIds.stream()
                        .collect(Collectors.toMap(AccessAuthPersonnel::getPersonnelId, AccessAuthPersonnel::getAccessAuthId));

                MultiResponse<PersonnelDTO> response = personnelService.getBatchPersonnel(personIds);
                List<PersonnelDTO> data = response.getData();
                Map<Long, String> icCodeMap = new HashMap<>();
                if(CollectionUtils.isNotEmpty(data)){
                    data.forEach(e->{
                        icCodeMap.put(e.getId(), e.getIccCardNum()); // 从原先的icCode改成iccCardNum
                    });
                }
                Map<Long, String> finalIcCodeMap = icCodeMap;
                accessAuthPersonnelVOS.forEach(e-> {
                    e.setAccessAuthId(map.get(e.getId()));
                    e.setIccCardNum(finalIcCodeMap.get(e.getId()));
                });
            }




            //拓展字段 - IC卡号查询
            /*MultiResponse<ExtendFieldValueDTO> response = personnelService.getExtendFieldByBizId(personIds.stream().collect(Collectors.toSet()), "personnel");
            List<ExtendFieldValueDTO> data = response.getData();
            if(CollectionUtils.isNotEmpty(data)){
                List<ExtendFieldValueDTO> extendList = data.stream()
                        .filter(e -> IC_CODE.equals(e.getFieldName()))
                        .collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(extendList)){
                    Map<Long, String> map = extendList.stream().collect(Collectors.toMap(ExtendFieldValueDTO::getBizId, ExtendFieldValueDTO::getValue));
                    accessAuthPersonnelVOS.forEach(e-> e.setIcCode(map.get(e.getId())));
                }
            }*/

        }
        return PageResponse.of(accessAuthPersonnelVOS,dtoPageResponse.getTotalCount(),
                dtoPageResponse.getPageSize(),
                dtoPageResponse.getPageIndex());
    }
}
